<?php
/*
 * Главная страница
 */
class Page extends BasicModel {
	public function __construct(Core $Site) {
		parent::__construct($Site);
	}
	public function makePage() {
		$Districts = $Area = NULL;
		// настройка параметров кэширования
		$this->Config->Cache['area.page']['CACHE_NAME'] .= $this->URL->Page['ID'];
		$this->MySQL->CacheSetConfig($this->Config->Cache['area.page']);
		// загрузка кэша
		$this->MySQL->CacheLoad();
		// область
		if (!$this->MySQL->CacheIsSet('AREA')) {
			$Area = $this->MySQL->Select('SELECT `ID`, `NAME`, `PAGE_H1` FROM `{p}areas` WHERE `ID` = '.$this->URL->Page['ID']);
			if ($Area) {$this->MySQL->CacheWrite($Area, 'AREA');}
			else {$this->Site->go404();}
		} else {$Area = $this->MySQL->CacheGet('AREA', TRUE);}
		// районы и города
		if (!$this->MySQL->CacheIsSet('DISTRICTS')) {
			$Select = $this->MySQL->Select('SELECT `d`.`ID`, `NAME`, `URL` FROM `{p}districts` `d` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `d`.`ID` WHERE `AREA_ID` = '.$Area['ID'].' AND `URL_TYPE` = 2', MySQL::SQuery);
			while (!is_null($District = $this->MySQL->FetchArray($Select))) {
				$Cities = $this->MySQL->Select('SELECT `NAME`, `URL` FROM `{p}cities` `c` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `c`.`ID` WHERE `DISTRICT_ID` = '.$District['ID'].' AND `URL_TYPE` = 3 ORDER BY `CITY_TYPE` ASC LIMIT 5', MySQL::SAll);
				$Districts[] = array(
					'ID'			=> $District['ID'],
					'NAME'			=> $District['NAME'],
					'URL'			=> $District['URL'],
					'CITIES'	=> $Cities
				);
			}
			if ($Districts) {$this->MySQL->CacheWrite($Districts, 'DISTRICTS');}
		} else {$Districts = $this->MySQL->CacheGet('DISTRICTS', TRUE);}
		// сохранение кэша
		//$this->MySQL->CacheSave();
		// настройка страницы
		$this->Template->setTitle($Area['NAME']); // тайтл
		$this->Template->setLoadPage('area.page'); // шаблон
		$this->Template->setPageElements(array ( // элементы страницы
			'AREA'			=> $Area,
			'DISTRICTS'	=> $Districts
		));
		$this->Template->addCrumbs(array (
			'/'	=> $this->Language->LText['MAIN'],
			''	=> $Area['NAME']
		));
		return 0;
	}
}
?>